// import { request } from "../../utils/util.js"
import {getLunbos,getMajor,getCourse,clickCourse,getTuijian} from "../../api/api.js"
import {showLoading} from "../../utils/util.js"

Page({
  data: {
    //轮播图数据
    lunbos:[],

    //专业加课程数据
    zhuanye1:[],


    //推荐
    tuijian:{},

    //自定义导航头
    th:0,
    mh:0,
    ml:0,
    mt:0
  },
 
  // 获取设备信息
  async getSystemInfo() {
    const res = await wx.getSystemInfo()
    let i = res.system.includes('iOS')?2:1
    const res1 = wx.getMenuButtonBoundingClientRect()
    const screenWidth = res.screenWidth
    this.setData({
      th:res1.top + res1.height + ((res1.top-res.statusBarHeight)*i),
      mh:res1.height,
      ml:screenWidth-res1.right,
      mt:res1.top
    })
  },

  //获取推荐课程
  getTuijian(){
    getTuijian().then(res=>{
      this.setData({
        tuijian:res.data.data[0]
      })
    })
  },

  //跳转领取资源页面
  tiaozhuanZY() {
    wx.switchTab({
      url: '../ziyuan/ziyuan',
    })
  },

  // 点击更多去视频库  对应的专业
  goSPk(e) {
    console.log(e.currentTarget.dataset);
    const zy = e.currentTarget.dataset
    wx.setStorageSync('zy', zy)
    wx.switchTab({
      url: '../shipinku/shipinku',
    })
  },


  //点击轮播图去课程
  goKc(e){
    const item = e.currentTarget.dataset.item
    console.log(e);
    wx.setStorageSync('kc', item)
    wx.navigateTo({
      url: '../../pages/kecheng/kecheng',
    })
  },

  //获取轮播图函数
  getLunbos(){
    getLunbos().then((res=>{
      this.setData({
        lunbos:res.data.data 
      })
    }))
  },

  //图片加载失败
  err(){
    wx.showToast({
      title: '图片加载失败，请刷新重试',
      icon:'none'
    })
  },

  //获取课程和专业函数
  async getList(){
    //调用获取专业接口
    const res1 = await getMajor()
    // 调用获取课程接口
    const res2 = await getCourse()
    let m = res1.data.data;
    let c = res2.data.data;
    //循环专业数组
    m.forEach(item=>{
      // //过滤课程数组 
      
        const ii = c.filter(res=>{
          // 如果课程中包含对应的专业 提取出来
            if(res.major_id){
              if(item._id === res.major_id._id){
                return res
              }
            }
              
        })
        //反转并截取
        item.list = ii.reverse().slice(0,6)
     
      
    })

    this.setData({
      zhuanye1:m
    })

    //关闭加载提示框
    wx.hideLoading()

  },



  //点击课程函数
  clickKc(e){
    //获取对应课程id
    const id = e.currentTarget.dataset.kc._id

    //获得对应课程访问量
    const count = e.currentTarget.dataset.kc.visit_count+1

    //调用修改访问量函数
    clickCourse(id,count).then(res=>{
      console.log(res); 
    }).catch(err=>{
      console.log(err);
    })
    
    wx.setStorageSync('kc', e.currentTarget.dataset.kc)

    wx.navigateTo({
      url: '../kecheng/kecheng',
    })

  },

  onLoad() {
    this.getSystemInfo()
    
    showLoading("加载中...")

    //调用获取轮播图函数
    this.getLunbos()

    //调用获取推荐专业函数
    this.getTuijian()

    //调用获取课程和专业函数
    this.getList()



  },
  onShow(){

    this.getList()
  },

  //下拉刷新
  onPullDownRefresh:function(){
  },

  //分享微信好友
  onShareAppMessage:function(from){
    if(from.from === 'menu'){
      return {
        title:'科云视频库，大量IT学习资源等你来',
        path:'/pages/index/index'
      }
    }
  },
  //分享朋友圈
  onShareTimeline:function(){
    return {
      title:'科云视频库，大量IT学习资源等你来',
      imageUrl:"https://7975-yueyun188-0gdwo4vh4afabc50-1306555544.tcb.qcloud.la/cloudbase-cms/upload/2022-03-09/mras7605qjl0klsizgkbvfga5nrpsian_.png",
      path:'/pages/index/index'
    }
  }
})
